import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class testResultSet {

	public static final String url = "jdbc:sqlserver://localhost:1433;DatabaseName=webxp";

	public static final String drivername = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

	public static final String user = "sa";

	public static final String password = "hdg";

	public static void main(String[] args) {
		String sql = "select UserName from XP_USER";
		Connection con = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			Class.forName(drivername);
			//System.out.println("加载驱动成功！");
			con = DriverManager.getConnection(url, user, password);
			
			System.out.println("连接数据库成功！url = " + url);
			statement = con.createStatement();
			resultSet = statement.executeQuery(sql);
			while (resultSet.next()){
				System.out.println("----------"+resultSet.getString(1));
			}

			resultSet.close();
			statement.close();
			con.close();

		} catch (Exception e) {
			e.printStackTrace(System.out);
		} finally {
			try {
				if (resultSet != null) {
					resultSet.close();
				}
			} catch (Exception e) {
				System.err.println("关闭ResultSet时发生异常！");
			}
			try {
				if (statement != null) {
					statement.close();
				}
			} catch (Exception e) {
				System.err.println("关闭statement时发生异常！");
			}
			try {
				if (con != null) {
					con.close();
				}
			} catch (Exception e) {
				System.err.println("关闭connection时发生异常！");
			}
		}
	}
}